package com.darrenchan.leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-09-11 19:14
 */
public class XieMatrix {
    public static void main(String[] args) {
        int[][] a = new int[][]{{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

        printMatrix(a);
//        print(a, 0, 2, 2, 0, true);
    }


    public static void printMatrix(int[][] a) {
        int i = 0, j = 0, i2 = 0, j2 = 0;
        int m = a.length, n = a[0].length;
        boolean f = true;
        while (i < m && j2 < n) {
            print(a, i, j, i2, j2, f);
            i = j == n - 1 ? i + 1 : i;
            j = j == n - 1 ? j : j + 1;
            j2 = i2 == m - 1 ? j2 + 1 : j2;
            i2 = i2 == m - 1 ? i2 : i2 + 1;

            f = !f;
        }
    }

    public static void print(int[][] a, int i, int j, int i2, int j2, boolean f) {
        if(f) {
            while (i <= i2 && j >= j2) {
                System.out.print(a[i][j] + " ");
                i++;
                j--;
            }
        } else {
            while (i2 >= i && j2 <= j) {
                System.out.print(a[i2][j2] + " ");
                i2--;
                j2++;
            }
        }
        System.out.println();
    }

//    public static void printMatrix(int[][] a) {
//        int m = a.length;
//        int n = a[0].length;
//
//        for (int i = n - 1; i >= 0; i--) {
//            int r = 0;
//            int c = i;
//
//            while(r >= 0 && r < m && c >= 0 && c < n){
//                System.out.print(a[r][c] + " ");
//                r++;
//                c++;
//            }
//            System.out.println();
//        }
//
//        for (int i = 1; i < m; i++) {
//            int r = i;
//            int c = 0;
//            while(r >= 0 && r < m && c >= 0 && c < n){
//                System.out.print(a[r][c] + " ");
//                r++;
//                c++;
//            }
//            System.out.println();
//        }
//    }
}
